home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / files.swg / 0078_Complete File Handling.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-09-04  |  15.8 KB  |  486 lines

  1. {
  2. Here's a unit I wrote to handle files and directories.  It has procedures
  3. similare to SetFAttr and GetFAttr, plus two others dealing with file
  4. attributes.  It also has a procedure to return a linked list of all the
  5. files in the current directory, three procedure to work with that (I may
  6. write one to sort it later), and one to dispose of the linked list.
  7.  
  8. At the end of the unit will be a program called attribs that uses it.  It's
  9. basically the same as DOS's attrib with some added features, such as:  It
  10. now works on directories too (i.e. you can now hide directorys), you can
  11. list only the files and directories with certain attributes set, you can
  12. list only directorys, etc...
  13.  
  14. As always, comments, flames, criticism (constructive or otherwise), and
  15. even "this sucks!" or "cool!" are welcome.
  16.  
  17.                                                 -Rick
  18. rick.haines@cde.com
  19. }
  20.  
  21. {$A+,B-,D-,E+,F-,G+,I+,L+,N-,O-,P-,Q-,R-,S-,T-,V+,X+,Y+}
  22. {$M 16384,0,655360}
  23. { ********************************************************** }
  24. { *********************** Files Unit *********************** }
  25. { ********************************************************** }
  26. { **************** Written by: Rick Haines ***************** }
  27. { **************************** rick.haines@cde.com ********* }
  28. { ********************************************************** }
  29. { ***************** Last Revised 03/29/95 ****************** }
  30. { ********************************************************** }
  31.  
  32. Unit Files;
  33.  
  34. Interface
  35.  
  36. Const
  37.  NormalF   = $0;          { Normal File   }
  38.  ReadOnlyF = $1;          { ReadOnly File }
  39.  HiddenF   = $2;          { Hidden File   }
  40.  SystemF   = $4;          { System File   }
  41.  VolLabel  = $8;          { Volume Label  }
  42.  SubDir    = $10;         { Sub Directory }
  43.  ArchiveF  = $20;         { Archive File  }
  44.  AllFiles  = $3F;         { All Files     }
  45. {Reserved  = $40;}
  46. {Reserved  = $80;}
  47.  fOK       = $0;          { No Error       }
  48.  fFileNF   = $2;          { File Not Found }
  49.  fPathNF   = $3;          { Path Not Found }
  50.  fAccessD  = $5;          { Access Denied  }
  51.  fgError   = $120;        { Other Error    }
  52.  
  53. Type
  54.  FileListP = ^FileListT;
  55.  FileListT = Record
  56.    Name : String[12];
  57.    Attr : Byte;
  58.    Size : LongInt;
  59.    Next : FileListP;
  60.   End;
  61.  
  62.  Function SetNewFileAttr(FileName : String; Attr : Byte) : Integer;
  63.   { Sets Attr, Clears what is already set }
  64.  Function SetFileAttr(FileName : String; Attr : Byte) : Integer;
  65.   { Sets Attr, leaves the rest }
  66.  Function ClearFileAttr(FileName : String; Attr : Byte) : Integer;
  67.   { Clears Attr, leaves the rest }
  68.  Function  GetFileAttr(FileName : String) : Byte;
  69.   { Returns Attr }
  70.  Function GetFileList : FileListP;
  71.   { Returns a Linked List of all files in current directory }
  72.  Procedure FilterAttr(Var List : FileListP; Attr : Byte);
  73.   { Filter out all files without Attr }
  74.  Procedure FilterName(Var List : FileListP; Name : String);
  75.   { Filter out all files that don't match Name }
  76.  Procedure FilterNameAttr(Var List : FileListP; Name : String; Attr : Byte);
  77.   { Last two Procedures Combined }
  78.  Procedure DisposeFileList(Var List : FileListP);
  79.   { Disposes of the Linked List }
  80.  
  81. Implementation
  82.  Uses Dos;
  83.  
  84.  Procedure NullString; Assembler;
  85. { DS:DX = Pascal String }
  86. { Return : DS:DX = Null String }
  87. {          AX = fOK, Success     }
  88.   Asm
  89.    Mov bx, dx
  90.    Mov cl, Byte Ptr ds:[bx] { Get Length      }
  91.    Mov ax, fFileNF          { Set Error       }
  92.    Cmp cl, 254              { Is it too long? }
  93.    JA @Done                 { Yes, then exit  }
  94.    Xor ch, ch
  95.    Add bx, cx               { Offset + Length        }
  96.    Inc bx                   { Next Byte              }
  97.    Mov Byte Ptr ds:[bx], 0  { Null Term. String      }
  98.    Inc dx                   { Get rid of length Byte }
  99.    Mov ax, fOK              { Return No Error        }
  100.   @Done:
  101.   End;
  102.  
  103.  Function SetNewFileAttr(FileName : String; Attr : Byte) : Integer; Assembler;
  104.   Asm
  105.    Push ds
  106.    Lds dx, FileName         { Pascal String of FileName          }
  107.    Call NullString          { Change to a Null String            }
  108.    Cmp ax, fOK              { Change OK?                         }
  109.    JA @Done                 { If not then Exit                   }
  110.    Mov ah, 43h              { Dos Function 43h, File Change Mode }
  111.    Mov al, 1                { Change Attributes                  }
  112.    Mov cl, Attr             { Set Whatever Attributes            }
  113.    Int 21h                  { Call Dos                           }
  114.    JC @Done                 { See if there was an error          }
  115.    Mov ax, fOK              { If Not, Then No Error              }
  116.   @Done:
  117.    Pop ds
  118.   End;
  119.  
  120.  Function SetFileAttr(FileName : String; Attr : Byte) : Integer; Assembler;
  121.   Asm
  122.    Push ds
  123.    Lds dx, FileName         { Pascal String of FileName          }
  124.    Call NullString          { Change to a Null String            }
  125.    Cmp ax, fOK              { Change OK?                         }
  126.    JA @Done                 { If not then Exit                   }
  127.    Mov ah, 43h              { Dos Function 43h, File Change Mode }
  128.    Mov al, 0                { Return Attributes                  }
  129.    Int 21h                  { Call Dos                           }
  130.    JC @Done                 { See if there was an error          }
  131.    Mov ah, 43h              { Dos Function 43h, File Change Mode }
  132.    Mov al, 1                { Set File Attributes                }
  133.    Or  cl, Attr             { Set Whatever Attributes            }
  134.    Int 21h                  { Call Dos                           }
  135.    JC @Done                 { See if there was an error          }
  136.    Mov ax, fOK              { If Not, Then No Error              }
  137.   @Done:
  138.    Pop ds
  139.   End;
  140.  
  141.  Function ClearFileAttr(FileName : String; Attr : Byte) : Integer; Assembler;
  142.   Asm
  143.    Push ds
  144.    Lds dx, FileName         { Pascal String of FileName          }
  145.    Call NullString          { Change to a Null String            }
  146.    Cmp ax, fOK              { Change OK?                         }
  147.    JA @Done                 { If not then Exit                   }
  148.    Mov ah, 43h              { Dos Function 43h, File Change Mode }
  149.    Mov al, 0                { Return Attributes                  }
  150.    Int 21h                  { Call Dos                           }
  151.    JC @Done                 { See if there was an error          }
  152.    Mov ah, 43h
  153.    Mov al, 1                { Set File Attributes                }
  154.    Mov bl, Attr             { bl := Attr                         }
  155.    Not bl                   { Not bl (Attr)                      }
  156.    And cl, bl               { Clear Whatever Attributes          }
  157.    Int 21h                  { Call Dos                           }
  158.    JC @Done                 { See if there was an error          }
  159.    Mov ax, fOK              { If Not, Then No Error              }
  160.   @Done:
  161.    Pop ds
  162.   End;
  163.  
  164.  Function  GetFileAttr(FileName : String) : Byte; Assembler;
  165.   Asm
  166.    Push ds                  { Push Data Segment                  }
  167.    Lds dx, FileName         { Pascal String of FileName          }
  168.    Call NullString          { Change to a Null String            }
  169.    Cmp ax, fOK              { Change OK?                         }
  170.    JA @Done                 { If not then Exit                   }
  171.    Mov ah, 43h              { Dos Function 43h, File Change Mode }
  172.    Mov al, 0                { Return Attributes                  }
  173.    Int 21h                  { Call Dos                           }
  174.    JC @Error                { See if there was an error          }
  175.    Mov ax, cx               { Return Attributes                  }
  176.    Jmp @Done
  177.   @Error:
  178.    Mov ax, fgError          { Return Error }
  179.   @Done:
  180.    Pop ds                   { Pop Data Segment }
  181.   End;
  182.  
  183.  Function GetFileList : FileListP;
  184.   Var
  185.    Dir  : SearchRec;
  186.    Temp,
  187.    Last : FileListP;
  188.    I    : Word;
  189.   Begin
  190.    FindFirst('????????.???', AllFiles, Dir);
  191.    New(Temp);
  192.    GetFileList := Temp;
  193.     Repeat
  194.      Temp^.Name := Dir.Name;
  195.      Temp^.Attr := Dir.Attr;
  196.      Temp^.Size := Dir.Size;
  197.      Last := Temp;
  198.      New(Temp^.Next);
  199.      Temp := Temp^.Next;
  200.      FindNext(Dir);
  201.     Until DosError <> 0;
  202.    Dispose(Temp);
  203.    Last^.Next := Nil;
  204.   End;
  205.  
  206.  Procedure RemoveLink(List : FileListP);
  207.   Var
  208.    Next : FileListP;
  209.   Begin
  210.    If List^.Next = Nil Then Exit;
  211.    Next := List^.Next^.Next;
  212.    Dispose(List^.Next);
  213.    List^.Next := Next;
  214.   End;
  215.  
  216.  Procedure FilterAttr(Var List : FileListP; Attr : Byte);
  217.   Var
  218.    Temp,
  219.    Last : FileListP;
  220.   Begin
  221.    If List = Nil Then Exit;
  222.    Last := List;
  223.    Temp := Last^.Next;
  224.    While Temp <> Nil Do
  225.     Begin
  226.      If Temp^.Attr And Attr <> Attr Then RemoveLink(Last)
  227.       Else Last := Last^.Next;
  228.      Temp := Last^.Next;
  229.     End;
  230.    Temp := List;
  231.    If Temp^.Attr And Attr <> Attr Then
  232.     Begin
  233.      New(Last);
  234.      Last := Temp^.Next;
  235.      Dispose(Temp);
  236.      Temp := Last;
  237.      List := Temp;
  238.     End;
  239.   End;
  240.  
  241.  Function EqualNames(S1, S2 : String) : Boolean; { Borrowed from SWAG }
  242.   Var
  243.    STmp1 : String[8];
  244.    STmp2 : String[3];
  245.    SS1, SS2 : String[12];
  246.    I : Integer;
  247.   Begin
  248.    STmp1 := Copy(S1, 1, Pos('.', S1+'.'))+'????????';
  249.    If (Pos('.', S1) > 1) Then STmp2 := Copy(S1, Pos('.', S1)+1, 3)+'???'
  250.     Else STmp2 := '???';
  251.    For I := 1 To 8 Do If STmp1[I] = '*' Then For I := I To 8 Do
  252.     STmp1[I] := '?';
  253.    For I := 1 To 3 Do If STmp2[I] = '*' Then For I := I To 3 Do
  254.     STmp2[I] := '?';
  255.    SS1 := STmp1+'.'+STmp2;
  256.    STmp1 := Copy(S2, 1, Pos('.', S2+'.'))+'????????';
  257.    If (Pos('.', S2) > 1) Then STmp2 := Copy(S2, Pos('.', S2)+1, 3)+'???'
  258.     Else STmp2 := '???';
  259.    For I := 1 To 8 Do If STmp1[I] = '*' Then For I := I To 8 Do
  260.     STmp1[I] := '?';
  261.    For I := 1 To 3 Do If STmp2[I] = '*' Then For I := I To 3 Do
  262.     STmp2[I] := '?';
  263.    SS2 := STmp1+'.'+STmp2;
  264.    EqualNames := False;
  265.    For I := 1 To 12 Do If (UpCase(SS1[I]) <> UpCase(SS2[I])) And
  266.     (SS2[I] <> '?') Then Exit;
  267.    EqualNames := True;
  268.   End;
  269.  
  270.  Procedure FilterName(Var List : FileListP; Name : String);
  271.   Var
  272.    Temp,
  273.    Last : FileListP;
  274.   Begin
  275.    If List = Nil Then Exit;
  276.    Last := List;
  277.    Temp := Last^.Next;
  278.    While Temp <> Nil Do
  279.     Begin
  280.      If Not EqualNames(Temp^.Name, Name) Then RemoveLink(Last)
  281.       Else Last := Last^.Next;
  282.      Temp := Last^.Next;
  283.     End;
  284.    Temp := List;
  285.    If Not EqualNames(Temp^.Name, Name) Then
  286.  
  287.     Begin
  288.      New(Last);
  289.      Last := Temp^.Next;
  290.      Dispose(Temp);
  291.      Temp := Last;
  292.      List := Temp;
  293.     End;
  294.   End;
  295.  
  296.  Procedure FilterNameAttr(Var List : FileListP; Name : String; Attr : Byte);
  297.   Begin
  298.    FilterName(List, Name);
  299.    FilterAttr(List, Attr);
  300.   End;
  301.  
  302.  Procedure DisposeFileList(Var List : FileListP);
  303.   Var
  304.    Temp,
  305.    Next : FileListP;
  306.   Begin
  307.    Temp := List;
  308.     While Temp <> Nil Do
  309.      Begin
  310.       Next := Temp^.Next;
  311.       Dispose(Temp);
  312.       Temp := Next;
  313.      End;
  314.    List := Nil;
  315.   End;
  316.  
  317. End.
  318.  
  319. { ---------------------------    TEST PROGRAM ------------------- }
  320.  
  321. {$A+,B-,D-,E+,F-,G+,I+,L+,N-,O-,P-,Q-,R-,S-,T-,V+,X+,Y+}
  322. {$M 16384,0,655360}
  323. { ********************************************************** }
  324. { ************************* Attribs ************************ }
  325. { ********************************************************** }
  326. { **************** Written by: Rick Haines ***************** }
  327. { **************************** rick.haines@cde.com ********* }
  328. { ********************************************************** }
  329. { ***************** Last Revised 03/29/95 ****************** }
  330. { ********************************************************** }
  331. Program Attribs;
  332.  Uses Files;
  333.  
  334. Var
  335.  Path      : String;
  336.  Lines,
  337.  SetAttr,
  338.  ClearAttr : Byte;
  339.  ListIt    : Boolean;
  340.  Directory,
  341.  TempDir   : FileListP;
  342.  
  343. Procedure HelpMe;
  344.  Begin
  345.   Writeln;
  346.   Writeln('Attribs v1.0a -- Written by Rick Haines.');
  347.   Writeln;
  348.   Writeln('Format is:');
  349.   Writeln(' Attribs [/L] [/D] [FileName] [R+|R-] [H+|H-] [S+|S-] [A+|A-] [D+]');
  350.   Writeln;
  351.   Writeln('WARNING:');
  352.   Writeln(' Without the /L switch, Attribs will change the attributes');
  353.   Writeln(' of files instead of listing them!');
  354.   Writeln;
  355.   Writeln('[/L] - List files & their attributes (If no params, it is assumed)');
  356.   Writeln('[/D] - Use with /L to list only directories and their attributes');
  357.   Writeln;
  358.   Writeln('[FileName] - File(s) to Change/List (WildCards Accepted)');
  359.   Writeln('             If not included it is assumed to be *.*    ');
  360.   Writeln;
  361.   Writeln('               Without /L              With /L       ');
  362.   Writeln('               ~~~~~~~~~~              ~~~~~~~       ');
  363.   Writeln('[R+|R-] - Make File(s) ReadOnly | View ReadOnly Files');
  364.   Writeln('[H+|H-] - Make File(s) Hidden   | View Hidden Files  ');
  365.   Writeln('[S+|S-] - Make File(s) System   | View System Files  ');
  366.   Writeln('[A+|A-] - Make File(s) Archive  | View Archive Files ');
  367.   Writeln('[D+]    - Change Dir Attribs    | Do Not Use With /L ');
  368.   Halt;
  369.  End;
  370.  
  371. Procedure ParseCommandLine;
  372.  Var
  373.   I   : Byte;
  374.   Par : String;
  375.  Begin
  376.   Path := '*.*';
  377.   If ParamCount < 1 Then
  378.    Begin
  379.     ListIt := True;
  380.     Exit;
  381.    End;
  382.   For I := 1 To ParamCount Do
  383.    Begin
  384.     Par := ParamStr(I);
  385.      Case UpCase(Par[1]) Of
  386.       'D' : Case Par[2] Of
  387.              '+' : ClearAttr := ClearAttr Or SubDir;
  388.              '-' : SetAttr := SetAttr Or SubDir;
  389.              Else Path := Par;
  390.             End;
  391.       'H' : Case Par[2] Of
  392.              '+' : SetAttr := SetAttr Or HiddenF;
  393.              '-' : ClearAttr := ClearAttr Or HiddenF;
  394.              Else Path := Par;
  395.             End;
  396.       'S' : Case Par[2] Of
  397.              '+' : SetAttr := SetAttr Or SystemF;
  398.              '-' : ClearAttr := ClearAttr Or SystemF;
  399.              Else Path := Par;
  400.             End;
  401.       'R' : Case Par[2] Of
  402.              '+' : SetAttr := SetAttr Or ReadOnlyF;
  403.              '-' : ClearAttr := SetAttr Or ReadOnlyF;
  404.              Else Path := Par;
  405.             End;
  406.       'A' : Case Par[2] Of
  407.              '+' : SetAttr := SetAttr Or ArchiveF;
  408.              '-' : ClearAttr := ClearAttr Or ArchiveF;
  409.              Else Path := Par;
  410.             End;
  411.       '/' : Case UpCase(Par[2]) Of
  412.              'L' : ListIt := True;
  413.              'D' : SetAttr := SetAttr Or SubDir;
  414.              '?' : HelpMe;
  415.              Else Path := Par;
  416.             End;
  417.       Else Path := Par;
  418.      End;
  419.    End;
  420.  End;
  421.  
  422. Function GetBit(Byte, Bit : Word) : Boolean;
  423.  Begin
  424.   Byte := Byte And (1 ShL Bit);
  425.   GetBit := (Byte = (1 ShL Bit));
  426.  End;
  427.  
  428. Procedure WriteAttr(Attr : Byte);
  429.  Begin
  430.   If GetBit(Attr, 0) Then Write('R') Else Write(' ');
  431.   If GetBit(Attr, 1) Then Write(' H') Else Write('  ');
  432.   If GetBit(Attr, 2) Then Write(' S') Else Write('  ');
  433.   If GetBit(Attr, 5) Then Write(' A') Else Write('  ');
  434.   If GetBit(Attr, 3) Then Write(' V') Else Write('  ');
  435.   If GetBit(Attr, 4) Then Write(' Dir') Else Write('    ');
  436.   Write('  ');
  437.  End;
  438.  
  439. Function ReadKey : Char; Assembler;
  440.  Asm
  441.   Mov ax, 0
  442.   Int 16h
  443.  End;
  444.  
  445. Begin
  446.  SetAttr := NormalF;
  447.  ClearAttr := NormalF;
  448.  ParseCommandLine;
  449.  Directory := GetFileList;
  450.  FilterName(Directory, Path);
  451.  Writeln;
  452.  If ListIt Then
  453.   Begin
  454.    Lines := 0;
  455.    FilterAttr(Directory, SetAttr);
  456.    TempDir := Directory;
  457.    If TempDir = Nil Then Writeln('No Files Found');
  458.    While TempDir <> Nil Do
  459.     Begin
  460.      WriteAttr(TempDir^.Attr);
  461.      Writeln(TempDir^.Name);
  462.      TempDir := TempDir^.Next;
  463.      Inc(Lines);
  464.      If Lines >= 24 Then
  465.       Begin
  466.        Write('--Press any key to continue--');
  467.        ReadKey;
  468.        Writeln;
  469.        Lines := 0;
  470.       End;
  471.     End;
  472.   End;
  473.  If Not ListIt Then
  474.   Begin
  475.    TempDir := Directory;
  476.    While TempDir <> Nil Do
  477.     Begin
  478.      TempDir^.Attr := (TempDir^.Attr And Not ClearAttr) Or SetAttr;
  479.      SetNewFileAttr(TempDir^.Name, TempDir^.Attr);
  480.      TempDir := TempDir^.Next;
  481.     End;
  482.    If Directory = Nil Then Writeln('No Files Found') Else Writeln('Success!');
  483.   End;
  484.  DisposeFileList(Directory);
  485. End.
  486.